package com.example.pathprotectsystem.service.impl;

import com.example.pathprotectsystem.mapper.EvaluateMapper;
import com.example.pathprotectsystem.mapper.PathMapper;
import com.example.pathprotectsystem.pojo.AskList;
import com.example.pathprotectsystem.pojo.PageBean;
import com.example.pathprotectsystem.pojo.Path;
import com.example.pathprotectsystem.pojo.PathLevelModel;
import com.example.pathprotectsystem.service.EvaluateService;
import com.example.pathprotectsystem.utils.LiQingCentCalculateUtil;
import com.example.pathprotectsystem.utils.PCILevelUtil;
import com.example.pathprotectsystem.utils.RQILevelUtil;
import com.example.pathprotectsystem.utils.ShuiniCentCalculateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private PathMapper pathMapper;

    @Override
    public List<PathLevelModel> getpathlevelall(AskList askList) {
        //创建PageBean对象
        PageBean<PathLevelModel> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String pathName = askList.getPathName();
        String pavementType = pathMapper.getpavementTypeBypathName(pathName);
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<PathLevelModel> asPCI = evaluateMapper.getpathlevelPCIall(pathName);
        List<PathLevelModel> asIRI = evaluateMapper.getpathlevelIRIall(pathName);
        System.out.println(asPCI);
        System.out.println(asIRI);
        List<PathLevelModel> as = new ArrayList<>();
        for(int i=0;i<asIRI.size();i++){ //求 RQI----------------------------------------------------
            PathLevelModel p = new PathLevelModel();
            String finishTime = asIRI.get(i).getFinishTime().substring(0,7);
            String stringdoubles = asIRI.get(i).getPqiLevel();
            List<String> stringdouble = Arrays.asList(stringdoubles.split(","));
            double avgIRI=0; //求平均 IRI
            for(int j=0;j<stringdouble.size();j++){
                avgIRI+=Double.parseDouble(stringdouble.get(j));
            }
            avgIRI = avgIRI/stringdouble.size();
            double avgRQI = 4.98-0.34*avgIRI;
            if(avgRQI<0)avgRQI=0; //特判负数
            p.setFinishTime(finishTime);
            p.setIri(avgIRI);
            p.setRqi(avgRQI);p.setRqiLevel(RQILevelUtil.getLevel(avgRQI));
            p.setPci(100);p.setPciLevel("A");
            as.add(p);
        }
        for(int i=0;i<asPCI.size();i++){ //求 PCI----------------------------------------------------
            String finishTime = asPCI.get(i).getFinishTime().substring(0,7);
            String stringdamageP = asPCI.get(i).getPqiLevel();
            String stringdamageTypes = asPCI.get(i).getDamageTypes();
            System.out.println("stringdamageTypes"+stringdamageTypes);
            List<String> damagePs = Arrays.asList(stringdamageP.split(","));
            List<String> damageTypes = Arrays.asList(stringdamageTypes.split(","));
            double PCI = 100;
            double cents = 0;
            boolean isDownCent = false;
            //算总扣分
            for(int j=0;j<damagePs.size();j++){
                double doubledamageP = Double.parseDouble(damagePs.get(j));
                if(pavementType.equals("沥青路面")){
                    cents += LiQingCentCalculateUtil.getDownCent(damageTypes.get(j),doubledamageP);
                }
                else{
                    cents += ShuiniCentCalculateUtil.getDownCent(damageTypes.get(j),doubledamageP);
                }
            }
            if(cents!=0)isDownCent = true;
            //算 PCI
            for(int j=0;j<damagePs.size()&&isDownCent;j++){
                double doubledamageP = Double.parseDouble(damagePs.get(j));
                double cent = 0 ;
                if(pavementType.equals("沥青路面")){
                    cent = LiQingCentCalculateUtil.getDownCent(damageTypes.get(j),doubledamageP);
                }
                else{
                    cent = ShuiniCentCalculateUtil.getDownCent(damageTypes.get(j),doubledamageP);
                }
                double u = cent / cents;
                double w = 3.0*u*u*u - 5.5*u*u + 3.5*u;
                PCI = PCI -cent * w;
            }
            System.out.println("i="+i+" "+PCI+" 总 "+cents);
            if(PCI<0)PCI=0;
            boolean f = true;
            for(int j = 0; j<as.size(); j++){
                if(as.get(j).getFinishTime().equals(finishTime)){
                    f=false;
                    as.get(j).setPci(PCI);
                    as.get(j).setPciLevel(PCILevelUtil.getLevel(PCI));
                    break;
                }
            }
            if(f){
                PathLevelModel p = new PathLevelModel();
                p.setFinishTime(finishTime);
                p.setPci(PCI);
                p.setPciLevel(PCILevelUtil.getLevel(PCI));
                p.setIri(0);p.setRqi(4.98);p.setRqiLevel("A");
                as.add(p);
                System.out.println("f=true,i="+i+" "+p);
            }
        }
        // 求 PQI
        double w1 = 0.6,w2 = 0.4,T = 20;
        for(int i=0;i<as.size();i++){
            double c = T*w1*as.get(i).getRqi() + as.get(i).getPci()*w2;
            as.get(i).setPqi(c);
            as.get(i).setPqiLevel(PCILevelUtil.getLevel(c));
            System.out.println(as.get(i));
        }

        return as;
    }
}
